package com.lz.sports.core

import android.widget.Toast
import com.lz.comm.SP
import com.lz.sports.R
import com.lz.sports.bean.ExamArea
import com.lz.sports.bean.ExamPlan
import com.lz.sports.bean.ExamProject
import com.lz.sports.bean.ExamSite
import com.lz.sports.utils.ImportsUtils
import com.lz.sports.utils.ToastUtils

/**
 * 整个项目管理类
 */
object ProjectManager {

    //考试计划
    var examPlan: ExamPlan? = null
        private set

    //当前切换的项目
    var currentExamProject: ExamProject? = null
        private set

    //当前考区
    var currentExamArea: ExamArea? = null
        private set

    //当前考点
    var currentExamSite: ExamSite? = null
        private set

    init {
        val examPlan = ImportsUtils.importsExamPlan()
        if (examPlan != null && examPlan.isNotEmpty()) {
            this.examPlan = examPlan
            val currentId = SP.getInt(K_CURRENT_EXAMINEE_PROJECT_ID, -1)
            var currentProject = examPlan.examProject(currentId)
            if (currentProject == null) {
                currentProject = examPlan.firstProject()
                SP.setInt(K_CURRENT_EXAMINEE_PROJECT_ID, currentProject.id)
            }
            currentExamProject = currentProject
            if (examPlan.examAreaList != null) {
                val currentAreaId = SP.getInt(K_CURRENT_EXAMINEE_AREA_ID, -1)
                this.currentExamArea = examPlan.examArea(currentAreaId)
            }
            if (this.currentExamArea != null && this.currentExamArea!!.examSiteList != null) {
                val currentSiteId = SP.getInt(K_CURRENT_EXAMINEE_SITE_ID, -1)
                if (currentSiteId >= 0) {
                    this.currentExamSite = this.currentExamArea!!.examSite(currentSiteId)
                }
            }
        }
    }

    //导入项目
    fun from(examPlan: ExamPlan?): Boolean {
        if (examPlan != null && examPlan.isNotEmpty()) {
            this.examPlan = examPlan
            val currentProject = examPlan.firstProject()
            SP.setInt(K_CURRENT_EXAMINEE_PROJECT_ID, currentProject.id)
            currentExamProject = currentProject
            return true
        }
        return false
    }

    fun clear() {
        examPlan = null
        currentExamProject = null
        currentExamArea = null
        currentExamSite = null
        SP.remove(K_CURRENT_EXAMINEE_PROJECT_ID)
        SP.remove(K_CURRENT_EXAMINEE_AREA_ID)
        SP.remove(K_CURRENT_EXAMINEE_SITE_ID)
    }

    //项目信息是否配置
    fun hasProjectInit(showToast: Boolean = true): Boolean {
        val hasInit = examPlan != null && currentExamProject != null
        if (!hasInit && showToast) ToastUtils.showMyToast(R.string.config_project_un_init)
        return hasInit
    }

    //考区考点是否配置
    fun hasExamAreaInit(showToast: Boolean = true): Boolean {
        if (currentExamArea == null) {
            if (showToast) ToastUtils.showMyToast(R.string.config_area_un_init)
            return false
        }
        if (currentExamSite == null) {
            if (showToast) ToastUtils.showMyToast(R.string.config_site_un_init)
            return false
        }
        return true
    }

    //当前时间是否在考试时间范围内
    fun hasInDateTime(): Boolean {
        val res = currentExamProject != null && currentExamProject!!.isEffective()
        if (!res) {
            ToastUtils.showMyToast(R.string.main_not_time, Toast.LENGTH_SHORT)
        }
        return res
    }

    //考试计划是否初始化好
    fun hasPlanInit(): Boolean = examPlan != null && currentExamArea != null && currentExamSite != null

    fun selectProject(position: Int): Boolean {
        val examProject = examPlan?.examProjectList?.getOrNull(position)
        if (examProject != null && examProject != currentExamProject) {
            this.currentExamProject = examProject
            SP.setInt(K_CURRENT_EXAMINEE_PROJECT_ID, examProject.id)
            return true
        }
        return false
    }

    fun selectExamArea(position: Int): Boolean {
        val examArea = examPlan?.examAreaList?.getOrNull(position)
        if (examArea != null && examArea != currentExamArea) {
            this.currentExamArea = examArea
            this.currentExamSite = null
            SP.setInt(K_CURRENT_EXAMINEE_AREA_ID, examArea.id)
            return true
        }
        return false
    }

    fun selectExamSite(position: Int): Boolean {
        val examSite = currentExamArea?.examSiteList?.getOrNull(position)
        if (examSite != null && examSite != currentExamSite) {
            this.currentExamSite = examSite
            SP.setInt(K_CURRENT_EXAMINEE_SITE_ID, examSite.id)
            return true
        }
        return false
    }

    fun examPlanId(): Int = examPlan?.id ?: 0

    fun examPlanCode(): String = examPlan?.planCode ?: ""

    fun examProjectId(): Int = currentExamProject?.id ?: 0

    fun examProjectName(): String = currentExamProject?.projectName ?: ""

    fun examAreaId(): Int = currentExamArea?.id ?: 0

    fun examAreaCode(): String = currentExamArea?.examAreaCode ?: ""

    fun examSiteId(): Int = currentExamSite?.id ?: 0

    fun examSiteCode(): String = currentExamSite?.examSiteCode ?: ""

    //人证阈值与身份证
    fun personThreshold(): Float = examPlan?.personThreshold ?: 0f

    //人脸阈值与报名匹配
    fun faceThreshold(): Float = examPlan?.faceThreshold ?: 0f

    //指纹
    fun fingerprintThreshold(): Float = examPlan?.fingerprintThreshold ?: 0f
}